<?php
##############################################
#
#   活动
# @ Time : 2017-1-8 - 下午9:20:27
# @ Author : JiangYongWei <1085562810@qq.com>
# @ Encoding : UTF-8 language : PHP
#
##############################################


namespace Admin\Model;

class AdvertModel extends AdminBaseModel{
	/*活动表  */
	protected $tableName = 'advert';	
	
	public function __construct($tableName=''){
		//初始化数据库对象
		if(!empty($tableName)){
			$this -> tableName = $tableName;
		}	
		parent::__construct($this -> tableName);
	}
	
	/**
	 * 获取广告位列表
	 */
	public function getAdvertPosition($name){
	    if($name){
	        $map['name'] = array('like','%'.$name.'%');
	    }
	    $model = M('advert_position');
	    $count = $model -> where($map)->count();// 查询满足要求的总记录数
	    $Page = new \Think\Page($count, C('listRows'));
	    $show = $Page->show();
	    $result = $model->where($map)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
	    return array($result, $show);
	}
	
	
	/**
	 * 获取一条广告位
	 */
	public function getAdvertPositionFind($id) {
	    $model = M('advert_position');
	    return $model->where("id = $id")->find();
	}
	
	/**
	 * 删除广告位
	 */
	public function deleteAdvertPosition($id) {
	    $admin_base_model = new AdminBaseModel('advert_position');
	    return $admin_base_model->deleteData(array('id'=>$id));
	}
	
	/**
	 * 获取广告位字段
	 */
	public function getPositionField(){
	    $model = M('advert_position');
	    return $model->where('is_open = 1')->field('id, name')->order('id desc')->select();
	}
	
	/**
	 * 获取广告位名称
	 */
	public function getAdvertPositionName($id){
	    $model = M('advert_position');
	    return $model->where("id = $id")->getField('name');
	}
	
	/**
	 * 获取广告列表
	 */
	public function getAdvert($name, $position_id, $media_type){
	    if($name) $map['name'] = array('like','%'.$name.'%');
		if($position_id) $map['position_id'] = $position_id;
	    if($media_type) $map['media_type'] = $media_type;
	    $count = $this -> where($map)->count();// 查询满足要求的总记录数
	    $Page = new \Think\Page($count, C('listRows'));
	    $show = $Page->show();
	    $result = $this->where($map)->order('sort asc, id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
	    return array($result, $show);
	}
	
	
	/**
	 * 获取一条广告
	 */
	public function getAdvertFind($id) {
	    return $this->where("id = $id")->find();
	}
	
	
	/**
	 * 删除广告
	 */
	public function deleteAdvert($id) {
	    $admin_base_model = new AdminBaseModel('advert');
	    return $admin_base_model->deleteData(array('id'=>$id));
	}
	
}